Skip to content

feat(agents): 供应商密钥只写不读,Lithe 保存供应商列表,修改前备份并可恢复 - #249

Merged
xiaoyumuxi merged 1 commit into
mainfrom
feat/agent-providers-keys-backup
Dec 2, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
feat/agent-providers-keys-backup

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联

改动

都是 API-45、agents 能力 0 内的补充,不升版本,没有新增插件可调用的宿主方法或通知。

一、密钥只写不读

  • agentProviders.apply 的 addProvider / updateProvider 可带 key;updateProvider 可带 clearKey。两者不能同时给,都不给表示密钥不变。
  • key 为 1–1024 字节,不含控制字符和换行,否则 agentProviders.invalidKey。上限按 Windows Credential Manager 单条 2560 字节留出余量。
  • 密钥只存在于这一次请求、Core 内存和交给插件 edit 的参数里。Core 不写日志、不进错误信息和事件,类型的 Debug 输出打码(有测试)。
  • agentProviders.read 的每个供应商新增 keySet(布尔)。没有任何字段能把密钥值读回来。

二、由 Lithe 保存的供应商列表

  • 清单 configuration.providers.listKeptBy:cli(默认)或 lithe。
  • lithe 用于配置里只能有一个生效的自定义接口的 CLI:用户保存的供应商列表由 Core 存在资源库目录的 saved-providers.json(仅所有者可读,不含密钥),密钥由插件用已有的 lithe/host/credentials/* 存进自己的命名空间。
  • 读取时,配置里生效的自定义接口按 baseUrl 对上已保存项,ID 和名称取已保存项,其余以回读的配置为准。每项新增 saved、origin(configuration / lithe)、可选 model;每个 Agent 新增 listKeptBy。
  • lithe 模式下:新增必须带 baseUrl;接口地址与已保存项重复是 agentProviders.endpointExists;删除当前生效的已保存项是 agentProviders.activeProvider。
  • Core 先存列表再调插件,插件失败时把列表改回。

三、修改前备份,可以恢复

  • apply 替换配置文件前,Core 把原字节存到资源库目录的 backups/<Agent 摘要>/:baseline/ 是自上次恢复以来第一次修改前的原样,后续修改不覆盖;history/ 留最近 20 份。备份失败则不修改(agentProviders.backupFailed)。
  • 目录名是摘要,插件起的名字不会变成路径。目录和文件都仅所有者可读写。
  • 备份是原文件的逐字节副本,可能含密钥:只在本机,不写日志,不发给插件,不经任何接口回答。回答里只有 backup {savedAtMs, modifiedSince}。
  • 新命令 agentProviders.restore {agent, discardLaterChanges?},只接受用户发起:文件在 Lithe 最后一次写入后被别的程序改过时以 agentProviders.changedSinceApply 拒绝,带 discardLaterChanges 才覆盖;恢复前把当前文件也存进历史;原本不存在的文件恢复时删除。
  • 新错误码 agentProviders.noBackup、changedSinceApply、restoreFailed。

日志

  • 新事件 agent.providers.restore_applied / restore_failed,只记文件数、耗时和错误码。

文档与契约

兼容

验证(macOS,本机)

命令 结果
cargo fmt --check 通过
cargo test -p lithe-agent-runtime -p lithe-plugin-agents -p lithe-core -p lithe-diagnostics 全部通过(agent-runtime 72 项,plugin-agents 20 项)
cargo clippy --workspace --all-targets --all-features -- -D warnings 通过
scripts/rust-core/verify-rust-core-comments.sh 通过
node shared/plugins/scripts/verify-docs.mjs 通过
node shared/plugins/scripts/sdk-e2e.mjs 通过

未执行:整个 workspace 的测试只跑了上面四个 crate;Windows 上的文件权限与恢复没有实机验证。测试里的密钥只用合成字符串。

agentProviders.apply 的 addProvider/updateProvider 可带 key、clearKey;configuration.providers 新增 listKeptBy;替换配置文件前由 Core 备份,新增 agentProviders.restore。agents 0 内的补充,不升能力版本。
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review December 2, 2026 05:26
@xiaoyumuxi
xiaoyumuxi merged commit 727a0be into main Dec 2, 2026
9 of 11 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 5 potential issues.

Devin Review

Comment on lines +329 to +334
for (id, file) in &record.files {
let (_, path) = files
.iter()
.find(|(declared, _)| declared == id)
.ok_or(RestoreRefusal::Failed)?;
let real = real_file(path).map_err(|_| RestoreRefusal::Failed)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Restore overwrites the wrong configuration

After an agent’s configuration directory changes, restore applies the old backup to the new directory. Confirming discardLaterChanges lets it overwrite the new configuration or delete it if the original file was absent.

Learn more

The backup record identifies a file only by its manifest ID. agent_paths resolves that ID using current environment values, so its path can differ from the one edited. edit_staged already has the actual path when it captures the original bytes. Restore checks the current bytes only when discard is false, and then applies the old baseline to the newly resolved path.

Example: Back up an agent’s config.json under one CLI home, then restart with a different CLI home. Restore reports later changes. Confirming discard replaces the second home’s configuration even though Lithe never edited it.

Recommended fix: Bind each baseline and last-written digest to the resolved file identity at apply time. Refuse a mismatched target independently of discardLaterChanges, and test switching an environment-based root between apply and restore.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +1342 to +1346
let listed = saved_after != saved_before;
if listed {
let _writing = self.lock_writing();
saved::store(&config, &agent_key, saved_after.clone())?;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Overlapping saves lose provider entries

When two additions for the same agent read the same saved list, apply_providers_checked replaces it twice. Both can succeed, but the last write drops the other provider. A failed edit’s rollback can also drop an intervening save.

Learn more

The host command reaches the runtime without serializing provider operations. apply_providers_checked reads and checks the list before locking, then store replaces the whole list for that agent. The separate rollback also replaces the list without checking for intervening changes.

Example: Two settings surfaces add different providers to the same agent. Both read the original list before either writes. The second write replaces the first addition while both requests report success.

Recommended fix: Serialize the full provider-list transaction per agent, or use revision-checked commits and rollbacks. Keep the library-wide write lock out of backend calls.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +359 to +376
for (id, real, current, baseline) in &targets {
keep_history(&directory, id, current.as_deref()).map_err(|_| RestoreRefusal::Failed)?;
let put_back = match baseline {
Some(bytes) => write_replacing(real, bytes),
None => match fs::remove_file(real) {
Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error),
_ => Ok(()),
},
};
if put_back.is_err() {
// Files already put back are no longer backed up; the rest are.
let _ = store(&directory, &remaining);
return Err(RestoreRefusal::Failed);
}
remaining.files.remove(id);
let _ = fs::remove_file(directory.join(BASELINE_DIRECTORY).join(file_key(id)));
}
store(&directory, &remaining).map_err(|_| RestoreRefusal::Failed)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Partial restore blocks future recovery

If keep_history fails after one file is restored, restore exits with the old backup record. That record names a deleted baseline, so retries cannot restore the remaining files.

Learn more

Restore preloads all recorded baselines before any file is restored. It removes each baseline from disk as soon as that file is restored, but does not persist the shortened record on the history-write error path. A later retry stops during preflight before reaching any remaining write, even with discard enabled. Failure to persist the shortened record has the same effect.

Example: An agent has backups of two configuration files. The first restore succeeds, but the disk has no room for the second file’s history entry. Freeing space does not unblock restore because the record still references the first file’s deleted baseline.

Recommended fix: Persist recoverable progress before deleting baseline files. Stage all required history entries before touching the live files, and cover a failure after the first replacement with a retry test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +220 to +225
let previous = load(&directory);
let mut record = previous.clone().unwrap_or_default();
let mut baseline = None;
if !record.files.contains_key(file_id) {
let path = directory.join(BASELINE_DIRECTORY).join(file_key(file_id));
let _ = fs::remove_file(&path);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Unreadable records erase original backups

When the backup record is unreadable or invalid, take treats the next provider edit as the first. It deletes the existing baseline, losing the original recovery point.

Learn more

load returns the same value for a missing record and for a record that cannot be read or parsed. That is adequate for hiding unavailable status, but take uses it to decide whether any earlier baseline exists. The next apply deletes the baseline at the known file ID before attempting to store a replacement.

Example: The backup record becomes invalid while the original baseline remains intact. Changing the model again replaces the original baseline with the currently edited version. Restore can no longer recover the original.

Recommended fix: Distinguish missing records from read and validation errors. Fail apply with backupFailed on an unreadable or invalid record, preserving existing baselines and history.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

let written = real
.parent()
.map_or(Ok(()), ensure_directory)
.and_then(|()| write_replacing(&real, &edited).map_err(|_| write_failed()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟥 Provider keys inherit shared permissions

When a provider edit adds a key to a readable CLI configuration, write_replacing preserves its existing permissions. Other local users with access to that file can read the key.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant